package net.sahv.bdyz.controller.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sahv.bdyz.model.Address;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.service.AddressService;
import net.sahv.bdyz.service.MemberService;

/**
 * address表
 * 
 * @author 薛国计
 *
 */
@Controller
@RequestMapping("web/address/")
public class AddressController {
	@Autowired
	private AddressService addressService;
	@Autowired
	private MemberService memberService;

	/**
	 * 查询收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addresslist", method = RequestMethod.GET)
	public String getVerifyCode(HttpServletRequest req, Model model) {
		//Member member = (Member) req.getSession().getAttribute("member");
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member != null) {
			List<Address> addresslist = addressService.selectAllByMid(member.getmId());
			model.addAttribute("addresslist", addresslist);
		} else {
			model.addAttribute("addresslist", "");
		}
		return "member/member/reciveAddress";
	}

	/**
	 * 订单确认时候增加收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月11日
	 * @return
	 */
	@RequestMapping(value = "insertReciveAddress", method = RequestMethod.GET)
	public String insertReciveAddress() {
		return "member/receiptRddress/insertReciveAddress";
	}

	/**
	 * 新增收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @param address
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "insertaddress", method = RequestMethod.POST)
	public ApiResult insertaddress(HttpServletRequest req, Address address, String sheng, String shi) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member == null) {
			result.msg.status = 2;
			result.msg.desc = "未登录！请登录";
			return result;
		}
		List<Address> addresslist = addressService.selectAllByMid(member.getmId());
		if (addresslist.size() >= 3) {
			result.msg.status = 3;
			result.msg.desc = "最多只能添加3条收货地址";
			return result;
		}
		
		if(addresslist.size()==0){
			address.setaDefault(1);
		}else{
			address.setaDefault(0);
		}
		address.setaAddress(sheng + "-" + shi);
		address.setmId(member.getmId());
		address.setaCreateTime(new Date());
		address.setaIsDelete(0);
		int i = addressService.insert(address);
		if (i > 0) {
			return result;
		}
		result.msg.status = 1;
		result.msg.desc = "操作失败！";
		return result;
	}

	/**
	 * 删除收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @param aId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deladdress", method = RequestMethod.POST)
	public ApiResult deladdress(HttpServletRequest req, Integer aId) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member == null) {
			result.msg.status = 2;
			result.msg.desc = "未登录！请登录";
			return result;
		}
		int i = addressService.delete(aId);
		if (i > 0) {
			return result;
		}
		result.msg.status = 1;
		result.msg.desc = "操作失败！";
		return result;
	}

	/**
	 * 查看收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @param aId
	 * @return
	 */
	@RequestMapping(value = "viewddress/{aId}", method = RequestMethod.GET)
	public String ediaddress(Model model, @PathVariable(value = "aId") Integer aId) {
		Address address = addressService.findOne(aId);
		model.addAttribute("address", address);
		return "member/member/updateReciveAddress";

	}

	/**
	 * 修改保存收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @param address
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateaddress", method = RequestMethod.POST)
	public ApiResult updateaddress(HttpServletRequest req, Address address, String sheng, String shi) {
		ApiResult result = new ApiResult();
		address.setaAddress(sheng + "-" + shi);
		int i = addressService.update(address);
		if (i > 0) {
			return result;
		}
		result.msg.status = 1;
		result.msg.desc = "操作失败！";
		return result;

	}

	/**
	 * 设置默认收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @param aId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setdefaultaddress", method = RequestMethod.POST)
	public ApiResult setdefaultaddress(HttpServletRequest req, Integer aId) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member == null) {
			result.msg.status = 2;
			result.msg.desc = "未登录！请登录";
			return result;
		}
		Address address = addressService.findOne(aId);
		Address addressdefault = addressService.finddefault(address.getmId());
		if (addressdefault != null) {
			addressdefault.setaDefault(0);
			addressService.update(addressdefault);
		}
		address.setaDefault(1);
		int i = addressService.update(address);
		if (i > 0) {
			result.msg.status=0;
			result.msg.desc ="操作成功！";
			return result;
		}
		result.msg.status = 1;
		result.msg.desc = "操作失败！";
		return result;

	}

	/**
	 * 查看默认收货地址
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "viewdefaultaddress", method = RequestMethod.POST)
	public ApiResult viewdefaultaddress(HttpServletRequest req) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		Address addressdefault = addressService.finddefault(member.getmId());
		result.data.put("addressdefault", addressdefault);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "chakanshuliang", method = RequestMethod.GET)
	public ApiResult chakanshuliang(HttpServletRequest req) {
		ApiResult result = new ApiResult();
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member == null) {
			result.msg.status = 2;
			result.msg.desc = "未登录！请登录";
			return result;
		}
		List<Address> addresslist = addressService.selectAllByMid(member.getmId());
		if (addresslist.size() >= 3) {
			result.msg.status = 3;
			result.msg.desc = "最多只能添加3条收货地址";
			return result;
		}
		return result;
	}
}
